package com.ruoyi.system.domain;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.system.utils.RespEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 * @author Mark sunlightcs@gmail.com
 */
public class RR extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    /** 成功 */
    public static final int SUCCESS = Constants.SUCCESS;

    /** 失败 */
    public static final int FAIL = Constants.FAIL;
    public RR setData(Object data) {
        put("data",data);
        return this;
    }

    //利用fastjson进行反序列化
    public <T> T getData(Class<T> typeReference) {
        Object data = get("data");	//默认是map
        String jsonString = JSON.toJSONString(data);
        T t = JSON.parseObject(jsonString, typeReference);
        return t;
    }

    public RR() {
        put("code", 200);
        put("msg", "success");
    }

    public static RR error() {
        return error(5000, "未知异常，请联系管理员");
    }

    public static RR error(String msg) {
        return error(500, msg);
    }
    public static RR error(RespEnum respEnum) {
        return error(respEnum.getCode(), respEnum.getMessage());
    }
    public static RR error(int code, String msg) {
        RR rr = new RR();
        rr.put("code", code);
        rr.put("msg", msg);
        return rr;
    }
    public static RR ok(RespEnum respEnum) {
        return ok(respEnum.getCode(), respEnum.getMessage());
    }
    public static RR ok(String msg) {
        RR rr = new RR();
        rr.put("msg", msg);
        return rr;
    }
    public static RR ok(int code, String msg) {
        RR rr = new RR();
        rr.put("code", code);
        rr.put("msg", msg);
        return rr;
    }
    public static RR ok(Map<String, Object> map) {
        RR rr = new RR();
        rr.putAll(map);
        return rr;
    }

    public static RR ok() {
        return new RR();
    }

    public RR put(String key, Object value) {
        super.put(key, value);
        return this;
    }
    public  Integer getCode() {

        return (Integer) this.get("code");
    }

}

